<?php


namespace app\api\wxpay;

class Sign
{
	public static function v2($params, $key, $type = "MD5")
	{
		ksort($params);
		$string = "";
		if (!empty($params)) {
			$array = [];
			foreach ($params as $k => $v) {
				if ($key != "sign") {
					$array[] = $k . "=" . $v;
				}
			}
			$string = implode("&", $array);
		}
		$string = $string . "&key=" . $key;
		if ($type == "HMAC-SHA256") {
			$string = hash_hmac("sha256", $string, $key);
		} else {
			$string = md5($string);
		}
		$result = strtoupper($string);
		return $result;
	}
}